package com.example.floristshop;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.floristshop.adapters.FlowerAdapter;
import com.example.floristshop.database.Flower;
import com.example.floristshop.utils.InitDatabaseUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SearchFragment extends Fragment {
    private SearchView searchView;
    private TextView searchReturn;
    private RecyclerView recyclerView;

    FlowerAdapter adapter;
    LinearLayoutManager manager;

    List<Flower> flowerList = new ArrayList<>();

    public SearchFragment() {
    }

    public static SearchFragment newInstance(String param1, String param2) {
        SearchFragment fragment = new SearchFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // init data
        flowerList = LitePal.findAll(Flower.class);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // get view
        View rootView = inflater.inflate(R.layout.fragment_search, container, false);
        searchView = (SearchView)rootView.findViewById(R.id.flower_search_view);
        searchReturn = (TextView)rootView.findViewById(R.id.search_return);
        recyclerView = (RecyclerView)rootView.findViewById(R.id.flower_list_view);

        // set view
        adapter = new FlowerAdapter(getActivity(), R.layout.flower_item, flowerList);
        manager = new LinearLayoutManager(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);

        searchReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flowerList = LitePal.findAll(Flower.class);
                FlowerAdapter new_adapter = new FlowerAdapter(getActivity(), R.layout.flower_item, flowerList);
                recyclerView.setAdapter(new_adapter);
                searchReturn.setVisibility(View.GONE);
                searchView.setQuery("",false);
            }
        });
        searchReturn.setVisibility(View.GONE);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                flowerList = LitePal.where("name like ? or type like ? or origin like ?","%"+query+"%", "%"+query+"%", "%"+query+"%").find(Flower.class);
                FlowerAdapter new_adapter = new FlowerAdapter(getActivity(), R.layout.flower_item, flowerList);
                recyclerView.setAdapter(new_adapter);
                searchReturn.setText("共找到 "+flowerList.size()+" 种符合条件的鲜花  <取消搜索>");
                searchReturn.setVisibility(View.VISIBLE);

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        return rootView;
    }
}